package com.apex.crm.service;

import com.apex.crm.base.BaseService;
import com.apex.crm.dao.CustomerContactMapper;
import com.apex.crm.query.CustomerContactQuery;
import com.apex.crm.utils.AssertUtil;
import com.apex.crm.vo.CustomerContact;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname CustomerContactService
 * @description TODO
 * @date 2021/6/9 15:41
 */

@Service
public class CustomerContactService extends BaseService<CustomerContact,Integer> {

    @Resource
    private CustomerContactMapper customerContactMapper;

    public CustomerContact selectByCusId(Integer cid) {
        return customerContactMapper.selectByCusId(cid);
    }

    public Map<String, Object> selectAll(CustomerContactQuery customerContactQuery) {
        Map<String,Object> map=new HashMap<String,Object>();
        PageHelper.startPage(customerContactQuery.getPage(),customerContactQuery.getLimit());
        PageInfo<CustomerContact> pageInfo=new PageInfo<CustomerContact>(customerContactMapper.selectAll(customerContactQuery));
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return  map;
    }

    //添加交往记录
    public void saveCustomerContact(CustomerContact customerContact) {
        customerContact.setIsValid(1);
        customerContact.setCreateDate(new Date());
        customerContact.setUpdateDate(new Date());
        //添加数据
        AssertUtil.isTrue(customerContactMapper.insert(customerContact) < 1,"交往记录添加失败!");

    }

    //更新交往记录
    public void updateCustomerContact(CustomerContact customerContact) {
        customerContact.setUpdateDate(new Date());
        AssertUtil.isTrue(customerContactMapper.updateByPrimaryKey(customerContact) < 1,"交往记录更新失败!");
    }

    //删除交往记录
    public void deleteCustomerContact(Integer id) {
        CustomerContact customerContact = customerContactMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(null == customerContact,"待删除的客户记录不存在!");
        AssertUtil.isTrue(customerContactMapper.deleteById(customerContact) < 1,"客户记录删除失败!");

    }
}
